const redis = require('../../config/redis');
class Countdown {
    /**
     * 倒计时
     * @api {POST}        /countdown                  倒计时
     * @apiName           /countdown
     * @apiGroup          Countdown
     * @apiParam          {String}         deviceId   设备id
     * @apiVersion 0.1.0
     * @apiSuccessExample Success-Response:
     * {
        "code": 0,
        "msg": "success",
        "data": {
            "timeOut": 1200
        }
     }
     */
    async countdown(ctx) {
        const {
            request: {
                body: { deviceId }
            },
            user: { User }
        } = ctx;

        if (!deviceId) {
            ctx.errors('参数错误');
        }
        const key = User.openId + deviceId;
        let timeOut = await redis.ttl(key);
        console.log(key);
        if (!timeOut) {
            ctx.success({ timeOut: 0 });
        } else {
            ctx.success({ timeOut: timeOut });
        }
    }
}
module.exports = new Countdown();
